package Animation;

import Game.Board;

import Game.InputManager;
import Game.Player;
import Game.TurnManager;
import java.util.ArrayList;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

public class HotTipGLEventListener implements GLEventListener
{
    private GL			gl;			// GL object that performs rendering
    private GLDrawable		glDrawable;		// surface upon which OpenGL is performed
    private GLU			glu;			// GLU object

    final int CANVASX = 700;
    final int CANVASY = 700;

    Board board;
    Square boardSquare;
    Square[][] gameBoard;
    Grid grid;
    Circle piece;

    ArrayList<Player> playerList;
    TurnManager turnManager;

    public HotTipGLEventListener()
    {
        turnManager = turnManager.getTurnManager();
        board = turnManager.getBoard();
        playerList = turnManager.getPlayerList();
        boardSquare = board.getBoardSquare();
        gameBoard = board.getBoard();
        grid = board.getGrid();

    }

    public void init(GLAutoDrawable drawable) {
        glDrawable = drawable;				// initialize the drawable
        gl = drawable.getGL();				// get the current GL object
        glu = new GLU();				// get the current GLU object

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    	// set display window color to black
        gl.glMatrixMode(GL.GL_PROJECTION);    		// set projection parameters to 2D

        gl.glLoadIdentity();			    		// sets projection matrix to identity
        glu.gluOrtho2D(0, CANVASX, 0, CANVASY); 	// set coordinate values
									// with vertices (0,0) for lower left corner
    }

    public void display(GLAutoDrawable arg0) {
        gl.glClear(GL.GL_COLOR_BUFFER_BIT );

        boardSquare.drawSquare(gl);
        drawBoard();
        Player currentPlayer = turnManager.getCurrentPlayer();

        piece = currentPlayer.getPiece();
        piece.drawCircle(gl, currentPlayer.getCurrentX(), currentPlayer.getCurrentY(), 10);
 
  
        grid.drawGrid(gl);
    }

    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
        
    }

    public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
        
    }
    
    public void drawBoard()
    {
      int spaceCountX = 0;
      int spaceCountY = 0;
      while(spaceCountX < gameBoard.length)
        {
            while(spaceCountY < gameBoard.length)
            {
                Square currentSquare = gameBoard[spaceCountX][spaceCountY];
                currentSquare.drawSquare(gl);

                spaceCountY++;
            }
            spaceCountY = 0;
            spaceCountX++;
        }
    }
}